$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var focus = true;
    var palletBatch = "";
    var warehouse = "";

    $(".head .print").on("click", function () {
        if (!palletBatch) {
            return $alert("请先扫描托号标签！");
        } else {
            loading_show("打印中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/pallet/printNumber',
                data: {
                    palletBatch: palletBatch
                },
                dataType: 'json',
                json: 'callback',
                success: function (res) {
                    if (res.success) {
                        $alert(palletBatch + "打印成功");
                    } else {
                        console.log(res);
                        loading_hide();
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    console.log(error);
                    loading_hide();
                    $alert(error.responseText);
                }
            });
        }
    });

    //搜索框 获取焦点
    $("#goods_id").focus();
    $("#goods_id").blur(function () {
        if (focus) {
            $("#goods_id").focus();
        }
    });

    $("div.page").on("click", "span.personal", function() {
        localStorage.removeItem("packMachine");
        focus = false;
        $("#goods_id").blur();
    });

    $(".panel-overlay").on("click", function () {
        focus = true;
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1000);
    });

    //根据扫描内容 得到 产品信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                $alert("请扫描打托标签！");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            $(this).val("");
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/unpallet/getPalletWarehouse',
                data: {
                    palletBatch: val,
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        $(".content .list-block .goodsList_ul").empty();
                        palletBatch = data[0].palletBatch;
                        for (var i = 0; i < data.length; i++) {
                            var data_html = '<li attr-data="' + data[i].batch + '"><div class="row no-gutter">' +
                                '<div class="col-90">' +
                                '<p class="p_none"><span class="id">' + data[i]._id + '</span></p>' +
                                '<p><span>托架号:</span><span class="sp goods_cptjh">' +
                                data[i].palletBatch + '</span></p><p><span>编号:</span><span class="sp goods_cpph">' +
                                data[i].batch + '</span></p><p><span>客户:</span><span class="sp goods_cpkh">' +
                                data[i].customerNameShort + '</span></p><p><span>规格:</span><span class="sp goods_cpgg">' +
                                data[i].inventorySpecification + '</span></p><p><span>毛重:</span><span class="sp goods_cpmz">' +
                                data[i].grossQuantity + '</span><span>皮重:</span><span class="sp goods_cppz">' +
                                data[i].tareQuantity + '</span><span>净重:</span><span class="sp goods_cpjz">' +
                                data[i].quantity + '</span></p></div>' +
                                '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                            $(".content .list-block .goodsList_ul").append(data_html);
                        }
                        $alert("扫描成功", 800);
                        badge_text();
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    // 点击 补料 按钮
    $(".foot").on("click", ".add_add", function () {
        if (!palletBatch) {
            return $alert("请先扫描托号标签！");
        } else {
            focus = false;
            $(".mengceng").show();
            $("div.div_pt").show();
            $("div.div_pt .input").val("");
            $("div.div_pt .cpph").val("").focus();
        }
    });
    //点击 弹框 取消
    $("div.div_pt .pt_quxiao").click(function () {
        $(".mengceng").hide();
        $("div.div_pt").hide();
        $("#goods_id").focus();
        warehouse = "";
        focus = true;
    });
    $("div.div_pt .cpph").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                $alert("请扫描产品标签！");
                setTimeout(function () {
                    pass = 1;
                    $("div.div_pt .cpph").val("").focus();
                }, 1400);
                return;
            }
            if ($(".content .list-block .goodsList_ul li[attr-data='" + val + "']").length == 1) {
                $alert("不可重复扫描");
                setTimeout(function () {
                    pass = 1;
                    $("div.div_pt .cpph").val("").focus();
                }, 1400);
                return
            }
            $(this).val("");
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/unpallet/getWarehouse',
                data: {
                    batch: val,
                    palletBatch: palletBatch
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        if ($(".content .list-block .goodsList_ul li[attr-data='" + data.batch + "']").length == 1) {
                            $alert("不可重复扫描");
                            setTimeout(function () {
                                pass = 1;
                                $("div.div_pt .cpph").val("").focus();
                            }, 1400);
                            return
                        }
                        warehouse = data;
                        $("div.div_pt .cpph").val(warehouse.batch);
                        $("div.div_pt .cpgg").val(warehouse.inventorySpecification);
                        $("div.div_pt .cpmz").val(warehouse.grossQuantity).focus();
                        $("div.div_pt .cppz").val(warehouse.tareQuantity);
                    } else {
                        setTimeout(function () {
                            $("div.div_pt .cpph").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });
    //点击 弹框 确定
    $("div.div_pt .pt_queding").click(function () {
        var cpmz = $("div.div_pt .cpmz").val();
        var cppz = $("div.div_pt .cppz").val();
        if (cpmz <= 0) {
            $alert("毛重应大于零");
            $("div.div_pt .cpmz").focus();
        } else if (cppz < 0) {
            $alert("皮重不可小于零");
            $("div.div_pt .cppz").focus();
        } else if (Number(cppz) >= Number(cpmz)) {
            $alert("皮重应小于毛重");
            $("div.div_pt .cppz").focus();
        } else {
            if (pass == 0) {
                return
            }
            pass = 0;
            loading_show("补料中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/unpallet/addWarehouse",
                data: {
                    palletBatch: palletBatch,
                    _id: warehouse._id,
                    grossQuantity: cpmz,
                    tareQuantity: cppz,
                    quantity: Number(accSub(cpmz, cppz)),
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        console.log("补料成功");
                        var data_html = '<li attr-data="' + warehouse.batch + '"><div class="row no-gutter">' +
                            '<div class="col-90">' +
                            '<p class="p_none"><span class="id">' + warehouse._id + '</span></p>' +
                            '<p><span>托架号:</span><span class="sp goods_cptjh">' +
                            palletBatch + '</span></p><p><span>编号:</span><span class="sp goods_cpph">' +
                            warehouse.batch + '</span></p><p><span>客户:</span><span class="sp goods_cpkh">' +
                            warehouse.customerNameShort + '</span></p><p><span>规格:</span><span class="sp goods_cpgg">' +
                            warehouse.inventorySpecification + '</span></p><p><span>毛重:</span><span class="sp goods_cpmz">' +
                            cpmz + '</span><p><span>皮重:</span><span class="sp goods_cppz">' +
                            cppz + '</span></p><p><span>净重:</span><span class="sp goods_cpjz">' +
                            Number(accSub(cpmz, cppz)) + '</span></p></div>' +
                            '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                        $(".content .list-block .goodsList_ul").prepend(data_html);
                        $alert(warehouse.batch + "补料成功！");
                        warehouse = "";
                        focus = true;
                        badge_text();
                        $(".mengceng").hide();
                        $("div.div_pt").hide();
                        $("#goods_id").focus();
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    if (error.status == 0) {
                        pass =1;
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(error.responseText);
                    }
                },
                // complete: function (XMLHttpRequest, status) {
                //     loading_hide();
                //     if (status == 'timeout') {
                //         $alert("请求超时，请重新提交！");
                //     }
                // }
            });
        }
    });

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li .col-10", function () {
        var jianhao = $(this).parent().parent();
        var id = jianhao.find("span.id").text();
        var batch = jianhao.find("span.goods_cpph").text();
        $.confirm('确定将' + batch + "从托中删除吗？", function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            loading_show("补料中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/unpallet/deleteWarehouse",
                data: {
                    palletBatch: palletBatch,
                    _id: id,
                    batch: batch
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        console.log("删除成功");
                        $alert(batch + "删除成功！");
                        jianhao.remove();
                        warehouse = "";
                        focus = true;
                        badge_text();
                        $("#goods_id").focus();
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    if (error.status == 0) {
                        pass =1;
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(error.responseText);
                    }
                },
                // complete: function (XMLHttpRequest, status) {
                //     loading_hide();
                //     if (status == 'timeout') {
                //         $alert("请求超时，请重新提交！");
                //     }
                // }
            });
        });
    });

    //点击 清空数据
    $(".foot").on("click", ".add_go", function () {
        if (!palletBatch) {
            return $alert("请先扫描托号标签！");
        }
        $.confirm("确认清除" + palletBatch + "全部数据吗？", function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            loading_show("全部清除中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/unpallet/clearPallet",
                data: {
                    palletBatch: palletBatch
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        console.log("托架全部清除成功");
                        $alert(palletBatch + "清除成功！");
                        setTimeout(function () {
                            window.location.href = "unpallet.html";
                        }, 1300);
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    if (error.status == 0) {
                        pass =1;
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        pass = 1;
                        loading_hide();
                        $alert(error.responseText);
                    }
                },
                // complete: function (XMLHttpRequest, status) {
                //     loading_hide();
                //     if (status == 'timeout') {
                //         $alert("请求超时，请重新提交！");
                //     }
                // }
            });
        })
    });

    // 点击 添加入库 numbers信息 传给后台
    $(document).on("click", ".foot .add_success", function () {
        if (!palletBatch) {
            return $alert("请先扫描托号标签！");
        }
        $alert("确认合托了！");
        setTimeout(function () {
            window.location.href = "unpallet.html";
        }, 1300);
    });

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        var allNetWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            allWeight = accAdd(allWeight, $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpmz").html());
            allNetWeight = accAdd(allNetWeight, $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpjz").html());
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
        $(".details span.good_netWeight").html(allNetWeight);
    }

    badge_text();

});